package Terminal.UI;

import java.awt.Dimension;

import javax.swing.JPanel;

public class PannelliAffiancatiHorizzontal extends PanelComponent{
	
	//private GridLayout gridlay;
	
	public PannelliAffiancatiHorizzontal(PanelComponent parent) {
		super(parent);
	pannellocomponente = new JPanel(null);
	
	}
	
	
	public JPanel createComponents() {
		return pannellocomponente ;
	}
	
	@Override
	public void add(PanelComponent figlio) {
		super.add(figlio);
		JPanel paneltemp = figlio.createComponents();
		Dimension size = figlio.getSize();
		paneltemp.setBounds(0,0,figlio.getSize().width,figlio.getSize().height);
	
		pannellocomponente.add(paneltemp);
		if(size_width<size.width){size_width=size.width;}
		setSize(size_width, size_height+size.height);
		//pannellocomponente.repaint();
	}
	
	public void addPosition(PanelComponent figlio, int x)
	{
		super.add(figlio);
		JPanel paneltemp= figlio.createComponents();
		//Dimension size = figlio.getSize();
		paneltemp.setBounds(x,0,figlio.getSize().width,figlio.getSize().height);
		
		pannellocomponente.add(paneltemp);
		/*
		if(size_width<size.width){size_width=size.width;}
		setSize(size_width, size_height+size.height);*/
	
		updateSize();
		pannellocomponente.validate();
		
		
		
	}
	public void addindex(PanelComponent figlio,int index,int x ){
		super.add(figlio,index);
		JPanel paneltemp= figlio.createComponents();
		//Dimension size = figlio.getSize();
		paneltemp.setBounds(x,0,figlio.getSize().width,figlio.getSize().height);
		
		pannellocomponente.add(paneltemp);

	
		updateSize();
		pannellocomponente.validate();
	}

	@Override
	
	public void remove(int index ){
		super.remove(index);
		pannellocomponente.remove(index);
		updateSize();
	}
	
	public void removeall(){
		super.removeall();
		pannellocomponente.removeAll();
		updateSize();
	}
	
	
	public void updateSize(){
		size_width=0;
		size_height=0;
		int i=0;
		Dimension d= new Dimension(size_width,size_height);
		Dimension d2 = new Dimension(size_width,size_height);
		
		for (PanelComponent pcelement: children) {
			d=pcelement.getSize();
			if(d.width+pannellocomponente.getComponent(i).getLocation().x>size_width){size_width=d.width+pannellocomponente.getComponent(i).getLocation().x;}
			if(d.height+pannellocomponente.getComponent(i).getLocation().y>size_height){size_height=d.height+pannellocomponente.getComponent(i).getLocation().y;}
			i++;
		}
		
		
		
		setSize(size_width, size_height);
		if(parent!=null)parent.updateSize();
		
	}
}
